Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Jan 11, 2026

Summary by CodeRabbit

  • Chores

    • Version bumped to 0.1.3 and authors field standardized.
    • CI expanded to validate many additional targets (macOS, iOS, Android, FreeBSD, musl, 32/64-bit Linux variants, WASM/WASI) and added a dedicated clippy check.
  • Bug Fixes

    • More robust, platform-specific errno handling and error mapping.
  • Compatibility

    • Several integer/math APIs now return the crate-standard error type for invalid inputs.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 11, 2026

📝 Walkthrough

Walkthrough

Widen CI target matrix and replace Rust toolchain setup; bump package version; add platform-specific errno get/set for multiple OSes; change several integer math functions to return crate::Result with EDOM; simplify bigint log conditionals; adjust cmath errno mapping.

Changes

Cohort / File(s) Summary
CI / Workflows
\.github/workflows/rust.yml
Replace actions-rs/toolchain@v1 with dtolnay/rust-toolchain@stable; add macOS/iOS cross-compilation checks; add exotic_targets job validating many non-default targets (i686, aarch64, musl, FreeBSD, WASM, WASI, Android); add clippy job that denies warnings.
Package metadata
Cargo.toml
Replace author with authors array; bump version 0.1.20.1.3.
Errno handling
src/err.rs
Expand set_errno / get_errno with per-target branches (Linux, Android, macOS, Windows, other Unixes, WASM/no-op); map errno values to Error using guarded comparisons rather than fixed numeric literals.
CMath errno mapping
src/cmath/misc.rs
Replace direct libc::EDOM/libc::ERANGE matches in phase(z) with guarded comparisons against Error::EDOM as i32 / Error::ERANGE as i32.
Math — BigInt logs
src/math/bigint.rs
Consolidate conditional checks in log_bigint, log2_bigint, and log10_bigint to a single if let Some(x) = n.to_f64() && x.is_finite() pattern before falling back to frexp_bigint path.
Math — Integer API
src/math/integer.rs
Change return types for isqrt, factorial, comb, and perm from Result<_, ()> to crate::Result<_> and return Err(crate::Error::EDOM) on invalid (negative) inputs; update docs/comments and internal magnitude handling.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I hopped through CI, targets galore,
Toolchain swapped and tests explore.
Errnos matched in every place,
Math returns errors with gentle grace.
Version bumped — a crunchy carrot score!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix build for various targets' directly reflects the main changes: updating CI workflows, adding cross-compilation checks for macOS/iOS, adding exotic target validation on Ubuntu, and fixing platform-specific errno handling for various targets.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In @Cargo.toml:
- Around line 1-8: The Cargo.toml version was increased only as a patch (0.1.2
-> 0.1.3) but this PR introduces public API changes (e.g., modified function
signatures in src/math/integer.rs), so update the package version to a minor
bump (0.2.0) instead of a patch to reflect the breaking change; edit the version
field in Cargo.toml from "0.1.3" to "0.2.0" and ensure any related changelog or
documentation is updated to indicate the API change.
🧹 Nitpick comments (3)
src/math/bigint.rs (1)

107-117: Let-chains are fine, but confirm MSRV (and consider crate::mul_add in fallback math)

  • The if let Some(x) = ... && x.is_finite() simplification looks good, but it does assume let-chains support; please confirm the crate’s intended MSRV/toolchain baseline.
  • In log_bigint/log10_bigint fallback paths, consider using crate::mul_add(a, b, c) for c + a*b to match project guidance on FMA-sensitive platforms (as per coding guidelines).
Possible `mul_add` tweak (fallback path)
-    let log_n = crate::m::log(x) + std::f64::consts::LN_2 * (e as f64);
+    let log_n = crate::mul_add(std::f64::consts::LN_2, e as f64, crate::m::log(x));
@@
-    Ok(crate::m::log10(x) + std::f64::consts::LOG10_2 * (e as f64))
+    Ok(crate::mul_add(std::f64::consts::LOG10_2, e as f64, crate::m::log10(x)))

Also applies to: 138-148, 159-169

.github/workflows/rust.yml (1)

60-102: Exotic target checks: solid coverage; consider consolidating apt installs (optional)

The target list is great. Optional: consolidate apt-get update + installs into fewer steps to reduce time/flakiness.

src/math/integer.rs (1)

780-855: Tests should assert the specific error type (EDOM), not just “some error”

Right now the Python-error branches only check rs.is_err(). Per learnings, tests should validate error types (ValueError → Error::EDOM, OverflowError → Error::ERANGE). Here, the expected error for negative inputs is Error::EDOM. Based on learnings, tests should be tightened accordingly.

Example tightening (one pattern)
-                Err(_) => {
-                    assert!(rs.is_err(), "isqrt({n}): py raised error but rs={rs:?}");
-                }
+                Err(_) => {
+                    assert_eq!(rs, Err(crate::Error::EDOM), "isqrt({n}): py raised error but rs={rs:?}");
+                }
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0d8dbaf and cb586ae.

📒 Files selected for processing (5)
  • .github/workflows/rust.yml
  • Cargo.toml
  • src/err.rs
  • src/math/bigint.rs
  • src/math/integer.rs
🧰 Additional context used
📓 Path-based instructions (2)
src/math/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

src/math/**/*.rs: Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Files:

  • src/math/bigint.rs
  • src/math/integer.rs
src/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Files:

  • src/math/bigint.rs
  • src/err.rs
  • src/math/integer.rs
🧠 Learnings (9)
📓 Common learnings
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math.rs : Create missing helpers if CPython has a helper we don't have yet, such as math_1_fn for Rust function pointers or special case handlers for specific functions.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.

Applied to files:

  • src/math/bigint.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Applied to files:

  • src/math/bigint.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.

Applied to files:

  • src/math/bigint.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/exponential.rs : Copy CPython's explicit special case handling for IEEE specials (NaN, Inf, etc.) exactly, including logic for cases like NaN**0 = 1.

Applied to files:

  • src/math/bigint.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/trigonometric.rs : In cmath functions that use both sin and cos of the same angle (cosh, sinh, exp, rect), use m::sincos(x) which returns (sin, cos) tuple instead of calling sin and cos separately to match CPython's macOS __sincos_stret behavior.

Applied to files:

  • src/math/bigint.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Applied to files:

  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : Tests must verify both correct values for Ok results and correct error types (EDOM vs ERANGE) for Err results. Python ValueError maps to Error::EDOM and OverflowError maps to Error::ERANGE.

Applied to files:

  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.

Applied to files:

  • src/math/integer.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build and test (macos-latest)
  • GitHub Check: Build and test (windows-latest)
  • GitHub Check: Check exotic targets
🔇 Additional comments (9)
src/err.rs (1)

24-55: Android errno symbol (libc::__errno) needs a quick compile confirmation

The cfg split and excluding Android from the unix fallback looks right, but please confirm libc::__errno() is available for your Android targets/toolchain (and returns a writable errno pointer as expected). This is a likely build-break point if the symbol differs across libc/bionic variations.

Also applies to: 58-89

.github/workflows/rust.yml (4)

30-36: Python 3.14 in CI: confirm availability on all runners

actions/setup-python@v5 with python-version: "3.14" may be fine, but it’s worth confirming it resolves consistently across ubuntu-latest, windows-latest, and macos-latest to avoid transient CI failures.


34-36: Toolchain action swap looks good

Switching to dtolnay/rust-toolchain@stable is a clean simplification.


45-59: macOS cross-target cargo check additions are a good fit for “build for various targets”

The Intel macOS + iOS target checks provide early signal without needing full link steps.


103-112: Clippy with -Dwarnings is a nice guardrail

This should help keep cross-platform portability changes tidy.

src/math/integer.rs (4)

81-89: isqrt EDOM on negative input is consistent with crate error model

This aligns with the crate::Result / crate::Error::EDOM convention.


234-253: factorial signature + EDOM handling look consistent

No issues with the updated return type and negative guard.


653-678: comb now returns typed EDOM errors—good

Guarding n < 0 || k < 0 with Err(crate::Error::EDOM) matches the documented behavior.


680-710: perm negative-k handling is correct

The Some(k) if k < 0 => Err(EDOM) case is a good catch.

Comment on lines 1 to 8
[package]
name = "pymath"
author = "Jeong, YunWon <[email protected]>"
authors = ["Jeong, YunWon <[email protected]>"]
repository = "https://github.com/RustPython/pymath"
description = "A binary representation compatible Rust implementation of Python's math library."
version = "0.1.2"
version = "0.1.3"
edition = "2024"
license = "PSF-2.0"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Patch version bump likely mismatches breaking public API changes elsewhere in the PR

0.1.2 -> 0.1.3 is a patch bump, but this PR changes public function signatures (e.g., src/math/integer.rs return types). If this crate is consumed externally, consider bumping the “minor” (0.2.0) instead to avoid surprising downstream breakage.

Proposed change
 [package]
 name = "pymath"
 authors = ["Jeong, YunWon <[email protected]>"]
 repository = "https://github.com/RustPython/pymath"
 description = "A binary representation compatible Rust implementation of Python's math library."
-version = "0.1.3"
+version = "0.2.0"
 edition = "2024"
 license = "PSF-2.0"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[package]
name = "pymath"
author = "Jeong, YunWon <[email protected]>"
authors = ["Jeong, YunWon <[email protected]>"]
repository = "https://github.com/RustPython/pymath"
description = "A binary representation compatible Rust implementation of Python's math library."
version = "0.1.2"
version = "0.1.3"
edition = "2024"
license = "PSF-2.0"
[package]
name = "pymath"
authors = ["Jeong, YunWon <[email protected]>"]
repository = "https://github.com/RustPython/pymath"
description = "A binary representation compatible Rust implementation of Python's math library."
version = "0.2.0"
edition = "2024"
license = "PSF-2.0"
🤖 Prompt for AI Agents
In @Cargo.toml around lines 1 - 8, The Cargo.toml version was increased only as
a patch (0.1.2 -> 0.1.3) but this PR introduces public API changes (e.g.,
modified function signatures in src/math/integer.rs), so update the package
version to a minor bump (0.2.0) instead of a patch to reflect the breaking
change; edit the version field in Cargo.toml from "0.1.3" to "0.2.0" and ensure
any related changelog or documentation is updated to indicate the API change.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/err.rs (1)

3-20: Portability: define Error::{EDOM,ERANGE} from libc::{EDOM,ERANGE} (and add #[repr(c_int)]).
Right now Error is hardcoded to 33/34 (Line 6-8). With the new guarded comparisons (Line 17-18), any platform where those values differ will mis-map errno.

Proposed fix
 use std::ffi::c_int;

 // The values are defined in libc
 #[derive(Debug, PartialEq, Eq)]
+#[repr(c_int)]
 pub enum Error {
-    EDOM = 33,
-    ERANGE = 34,
+    EDOM = libc::EDOM,
+    ERANGE = libc::ERANGE,
 }
.github/workflows/rust.yml (1)

29-44: Remove unused matrix.rust variable and note Python 3.14 won't be pre-cached.

Line 20 defines rust: [stable] in the matrix, but it's never referenced in the workflow steps, so it should be removed. Additionally, Python 3.14 is not yet pre-cached in GitHub Actions hosted runners as of January 2026—actions/setup-python@v5 will download and install it on-the-fly, which adds unnecessary setup time. Consider using a Python version that's in the toolcache (e.g., 3.12), or if 3.14 is required, accept the slower installation step.

🤖 Fix all issues with AI agents
In @src/math/integer.rs:
- Around line 81-89: The isqrt implementation now returns
Err(crate::Error::EDOM) for negative inputs; update the tests (e.g.,
test_isqrt_impl) to assert the specific error variant rather than only checking
rs.is_err(), by invoking isqrt(-1) (or equivalent negative BigInt) and asserting
it equals Err(crate::Error::EDOM) or matches Error::EDOM; ensure you reference
the isqrt function and the crate::Error::EDOM variant in the assertion so
regressions to other error kinds (like ERANGE) will fail the test.
🧹 Nitpick comments (3)
.github/workflows/rust.yml (2)

45-59: macOS cross-target checks look reasonable; ensure mul_add feature is intended for all macOS target checks.
Because these checks reuse ${{ matrix.features }}, macOS will also cargo check iOS with mul_add enabled (Line 51-58). If mul_add is meant to be macOS-host-only, consider splitting features for host vs cross-target checks.


103-112: Clippy job is good; consider linting the macOS-only mul_add feature too.
Right now clippy runs complex,malachite-bigint only (Line 111-112), so warnings behind mul_add may be missed.

src/err.rs (1)

24-54: Minor: consider deduplicating errno pointer retrieval to avoid drift between set_errno and get_errno.
Today, the per-target selection logic is duplicated; a small errno_ptr() helper per platform would reduce the chance of future mismatches.

Also applies to: 56-88

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cb586ae and 19184be.

📒 Files selected for processing (6)
  • .github/workflows/rust.yml
  • Cargo.toml
  • src/cmath/misc.rs
  • src/err.rs
  • src/math/bigint.rs
  • src/math/integer.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • Cargo.toml
  • src/math/bigint.rs
🧰 Additional context used
📓 Path-based instructions (3)
src/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Files:

  • src/cmath/misc.rs
  • src/err.rs
  • src/math/integer.rs
src/cmath/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

src/cmath/**/*.rs: For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.
Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.

Files:

  • src/cmath/misc.rs
src/math/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

src/math/**/*.rs: Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Files:

  • src/math/integer.rs
🧠 Learnings (11)
📓 Common learnings
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math.rs : Create missing helpers if CPython has a helper we don't have yet, such as math_1_fn for Rust function pointers or special case handlers for specific functions.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/trigonometric.rs : In cmath functions that use both sin and cos of the same angle (cosh, sinh, exp, rect), use m::sincos(x) which returns (sin, cos) tuple instead of calling sin and cos separately to match CPython's macOS __sincos_stret behavior.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : All float functions must be tested with crate::test::EDGE_VALUES which includes zeros, infinities, NaNs with different payloads, subnormals, boundary values (MIN_POSITIVE, MAX, MIN), large values near infinity, and trigonometric special values (PI, PI/2, PI/4, TAU).
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Applied to files:

  • src/cmath/misc.rs
  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.

Applied to files:

  • src/cmath/misc.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.

Applied to files:

  • src/cmath/misc.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : Tests must verify both correct values for Ok results and correct error types (EDOM vs ERANGE) for Err results. Python ValueError maps to Error::EDOM and OverflowError maps to Error::ERANGE.

Applied to files:

  • src/cmath/misc.rs
  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/trigonometric.rs : In cmath functions that use both sin and cos of the same angle (cosh, sinh, exp, rect), use m::sincos(x) which returns (sin, cos) tuple instead of calling sin and cos separately to match CPython's macOS __sincos_stret behavior.

Applied to files:

  • src/cmath/misc.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.

Applied to files:

  • src/cmath/misc.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math.rs : Create missing helpers if CPython has a helper we don't have yet, such as math_1_fn for Rust function pointers or special case handlers for specific functions.

Applied to files:

  • src/cmath/misc.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : All float functions must be tested with crate::test::EDGE_VALUES which includes zeros, infinities, NaNs with different payloads, subnormals, boundary values (MIN_POSITIVE, MAX, MIN), large values near infinity, and trigonometric special values (PI, PI/2, PI/4, TAU).

Applied to files:

  • src/cmath/misc.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/exponential.rs : Copy CPython's explicit special case handling for IEEE specials (NaN, Inf, etc.) exactly, including logic for cases like NaN**0 = 1.

Applied to files:

  • src/cmath/misc.rs
  • src/err.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Applied to files:

  • src/cmath/misc.rs
  • src/math/integer.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build and test (macos-latest)
  • GitHub Check: Build and test (windows-latest)
  • GitHub Check: Check exotic targets
🔇 Additional comments (7)
.github/workflows/rust.yml (1)

60-102: Exotic targets: Android check may need an NDK/linker depending on build scripts—verify.
cargo check --target aarch64-linux-android (Line 76-79) can still fail if any dependency/build script needs a target linker or C toolchain; you currently don’t install an NDK. Also consider doing apt-get update before later installs (Line 85, 93) for resilience.

src/cmath/misc.rs (1)

20-28: phase() errno mapping change is fine, but it relies on Error::EDOM/ERANGE matching platform errno.
The guard comparisons (Line 25-26) are only as portable as the Error discriminants; ensure Error::{EDOM,ERANGE} are defined from platform libc constants (see src/err.rs). As per coding guidelines, good use of set_errno(0) before the libm call.

src/math/integer.rs (3)

234-253: factorial: return type + EDOM on negative input looks correct.
The early return on n < 0 (Line 240-242) matches the new error model.


653-678: comb: EDOM on negative inputs + 0 when k > n matches CPython semantics.
The guard at Line 659-661 is clear and consistent with the crate-wide error handling.


680-710: perm: negative handling is correct (including Some(k) if k < 0).
The match arm at Line 694-696 cleanly covers negative k without extra branches.

src/err.rs (2)

90-108: is_error() mapping looks consistent with existing underflow behavior.
The ERANGE “underflow to ~0” exception (Line 97-106) is preserved and the guarded comparisons align with the refactor.


24-54: FFI syntax safe fn in extern blocks is stable and supported—no changes needed.

The safe fn _errno() declaration inside extern "C" blocks is supported on stable Rust since version 1.82 (October 2024). The code correctly uses set_errno(0) to clear errno before libm calls, which aligns with proper error handling practices. The project has no MSRV constraint that would prevent this syntax, and there are no compilation concerns on Windows.

Likely an incorrect or invalid review comment.

@youknowone youknowone marked this pull request as draft January 11, 2026 14:24
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/err.rs (1)

59-88: Platform-specific errno retrieval looks correct.

The platform-specific implementations match the set_errno pattern correctly.

♻️ Optional: Deduplicate Windows FFI declaration

The _errno() FFI declaration is duplicated in both set_errno and get_errno. Consider extracting it to a module-level declaration:

+#[cfg(target_os = "windows")]
+unsafe extern "C" {
+    safe fn _errno() -> *mut i32;
+}
+
 /// Set errno to the given value.
 #[inline]
 pub(crate) fn set_errno(value: i32) {
     ...
     #[cfg(target_os = "windows")]
     unsafe {
-        unsafe extern "C" {
-            safe fn _errno() -> *mut i32;
-        }
         *_errno() = value;
     }
     ...
 }

 /// Get the current errno value.
 #[inline]
 pub(crate) fn get_errno() -> i32 {
     ...
     #[cfg(target_os = "windows")]
     unsafe {
-        unsafe extern "C" {
-            safe fn _errno() -> *mut i32;
-        }
         *_errno()
     }
     ...
 }
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 19184be and b5b2a3f.

📒 Files selected for processing (6)
  • .github/workflows/rust.yml
  • Cargo.toml
  • src/cmath/misc.rs
  • src/err.rs
  • src/math/bigint.rs
  • src/math/integer.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/cmath/misc.rs
🧰 Additional context used
📓 Path-based instructions (2)
src/math/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

src/math/**/*.rs: Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Files:

  • src/math/bigint.rs
  • src/math/integer.rs
src/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Files:

  • src/math/bigint.rs
  • src/err.rs
  • src/math/integer.rs
🧠 Learnings (11)
📓 Common learnings
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math.rs : Create missing helpers if CPython has a helper we don't have yet, such as math_1_fn for Rust function pointers or special case handlers for specific functions.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/trigonometric.rs : In cmath functions that use both sin and cos of the same angle (cosh, sinh, exp, rect), use m::sincos(x) which returns (sin, cos) tuple instead of calling sin and cos separately to match CPython's macOS __sincos_stret behavior.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : All float functions must be tested with crate::test::EDGE_VALUES which includes zeros, infinities, NaNs with different payloads, subnormals, boundary values (MIN_POSITIVE, MAX, MIN), large values near infinity, and trigonometric special values (PI, PI/2, PI/4, TAU).
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.

Applied to files:

  • src/math/bigint.rs
  • src/err.rs
  • Cargo.toml
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/exponential.rs : Copy CPython's explicit special case handling for IEEE specials (NaN, Inf, etc.) exactly, including logic for cases like NaN**0 = 1.

Applied to files:

  • src/math/bigint.rs
  • src/err.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Applied to files:

  • src/math/bigint.rs
  • Cargo.toml
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.

Applied to files:

  • src/math/bigint.rs
  • Cargo.toml
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/trigonometric.rs : In cmath functions that use both sin and cos of the same angle (cosh, sinh, exp, rect), use m::sincos(x) which returns (sin, cos) tuple instead of calling sin and cos separately to match CPython's macOS __sincos_stret behavior.

Applied to files:

  • src/math/bigint.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math.rs : Create missing helpers if CPython has a helper we don't have yet, such as math_1_fn for Rust function pointers or special case handlers for specific functions.

Applied to files:

  • src/math/bigint.rs
  • Cargo.toml
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Applied to files:

  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : Tests must verify both correct values for Ok results and correct error types (EDOM vs ERANGE) for Err results. Python ValueError maps to Error::EDOM and OverflowError maps to Error::ERANGE.

Applied to files:

  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.

Applied to files:

  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : All float functions must be tested with crate::test::EDGE_VALUES which includes zeros, infinities, NaNs with different payloads, subnormals, boundary values (MIN_POSITIVE, MAX, MIN), large values near infinity, and trigonometric special values (PI, PI/2, PI/4, TAU).

Applied to files:

  • src/math/integer.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build and test (macos-latest)
  • GitHub Check: Build and test (windows-latest)
  • GitHub Check: Check exotic targets
🔇 Additional comments (15)
Cargo.toml (1)

3-3: LGTM! Version bump and metadata update.

The version increment to 0.1.3 and the authors field addition are appropriate for this release.

Also applies to: 6-6

src/math/bigint.rs (3)

113-117: LGTM! Clean refactoring using let-chains.

The combined conditional if let Some(x) = n.to_f64() && x.is_finite() is more concise and idiomatic than the previous two-step pattern. The logic and behavior remain unchanged.


144-148: LGTM! Consistent with log_bigint refactoring.

Same improvement as in log_bigint using let-chains.


165-169: LGTM! Consistent with other log functions.

Same improvement as in log_bigint and log2_bigint using let-chains.

.github/workflows/rust.yml (4)

35-35: LGTM! Modernized Rust toolchain setup.

Switching from actions-rs/toolchain@v1 (which is deprecated) to dtolnay/rust-toolchain@stable is the recommended approach.


45-58: LGTM! macOS cross-compilation validation.

Adding checks for Intel macOS (x86_64-apple-darwin) and iOS (aarch64-apple-ios) targets ensures compatibility across Apple platforms.


60-113: LGTM! Comprehensive exotic target coverage.

The new job validates builds across diverse platforms (32-bit Linux, Android, ARM64, musl, FreeBSD, WASM), directly addressing the PR objective to fix builds for various targets.


115-124: LGTM! Clippy enforcement.

Adding a dedicated clippy job with -Dwarnings helps maintain code quality.

src/math/integer.rs (4)

83-88: LGTM! Proper error handling migration.

The migration from Result<BigInt, ()> to crate::Result<BigInt> with Err(crate::Error::EDOM) for negative inputs is correct and aligns with the coding guidelines. As per coding guidelines, returning Result directly with Err(crate::Error::EDOM) is the recommended approach.


236-241: LGTM! Consistent error handling for factorial.

The error handling update matches the pattern in isqrt, correctly using EDOM for negative input domain errors. As per coding guidelines, this is the correct approach.


655-660: LGTM! Consistent error handling for comb.

The error handling update correctly uses EDOM for domain errors (negative n or k). As per coding guidelines, this is the correct approach.


682-694: LGTM! Consistent error handling for perm.

The error handling update correctly uses EDOM for both negative n and negative k cases. As per coding guidelines, this is the correct approach.

src/err.rs (3)

17-18: LGTM! Guards ensure consistency with enum values.

Using guards (x if x == Error::EDOM as c_int) instead of hardcoded literals ensures the match arms stay synchronized with the enum discriminants.


95-96: LGTM! Guards replace hardcoded errno constants.

Using guards (e if e == Error::EDOM as i32) is consistent with the TryFrom implementation and more maintainable than hardcoded libc constants.


27-54: Platform-specific errno handling is correct.

The implementations for all platforms are appropriate: Linux via __errno_location(), Android via __errno(), macOS and BSD variants via __error(), Windows via _errno(), and WASM as a no-op. All set_errno calls in the codebase correctly pass 0 to clear errno before libm calls, and error reporting uses Result with Err(Error::EDOM) or Err(Error::ERANGE) as required. Tests properly verify both correct return values and error type conversions (ValueError → EDOM, OverflowError → ERANGE).

@youknowone youknowone marked this pull request as ready for review January 12, 2026 06:34
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/err.rs (2)

31-54: iOS target is not explicitly handled in set_errno.

iOS (target_os = "ios") will fall through to the generic Unix branch using libc::__error(), which should work since iOS uses the same function as macOS. However, for clarity and to avoid potential issues, consider adding explicit iOS handling.

Suggested fix to add explicit iOS handling
     #[cfg(target_os = "macos")]
     unsafe {
         *libc::__error() = value;
     }
+    #[cfg(target_os = "ios")]
+    unsafe {
+        *libc::__error() = value;
+    }
     #[cfg(target_os = "windows")]
     unsafe {
         *_errno() = value;
     }
-    #[cfg(all(unix, not(any(target_os = "linux", target_os = "android", target_os = "macos"))))]
+    #[cfg(all(unix, not(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))))]
     unsafe {
         // FreeBSD, NetBSD, OpenBSD, etc. use __error()
         *libc::__error() = value;
     }

60-85: Same iOS handling consideration applies to get_errno.

The get_errno function has the same pattern as set_errno. iOS will work via the generic Unix fallback, but explicit handling would be clearer.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b5b2a3f and ce62d41.

📒 Files selected for processing (6)
  • .github/workflows/rust.yml
  • Cargo.toml
  • src/cmath/misc.rs
  • src/err.rs
  • src/math/bigint.rs
  • src/math/integer.rs
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/math/bigint.rs
  • src/cmath/misc.rs
  • Cargo.toml
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Files:

  • src/err.rs
  • src/math/integer.rs
src/math/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

src/math/**/*.rs: Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.

Files:

  • src/math/integer.rs
🧠 Learnings (8)
📓 Common learnings
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math.rs : Create missing helpers if CPython has a helper we don't have yet, such as math_1_fn for Rust function pointers or special case handlers for specific functions.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use crate::mul_add(a, b, c) instead of a * b + c for expressions of the form a * b + c or c + a * b to match CPython's FMA (fused multiply-add) behavior on platforms like macOS where it's enabled.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/trigonometric.rs : In cmath functions that use both sin and cos of the same angle (cosh, sinh, exp, rect), use m::sincos(x) which returns (sin, cos) tuple instead of calling sin and cos separately to match CPython's macOS __sincos_stret behavior.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : All float functions must be tested with crate::test::EDGE_VALUES which includes zeros, infinities, NaNs with different payloads, subnormals, boundary values (MIN_POSITIVE, MAX, MIN), large values near infinity, and trigonometric special values (PI, PI/2, PI/4, TAU).
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*.rs : Error handling: Return Result directly with Err(crate::Error::EDOM) or Err(crate::Error::ERANGE) instead of using set_errno. Only valid set_errno call is set_errno(0) to clear errno before libm calls.

Applied to files:

  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : Tests must verify both correct values for Ok results and correct error types (EDOM vs ERANGE) for Err results. Python ValueError maps to Error::EDOM and OverflowError maps to Error::ERANGE.

Applied to files:

  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/**/*.rs : Use existing CPython helper equivalents: FUNC1 → math_1(x, func, can_overflow), FUNC2 → math_2(x, y, func), m_log/m_log10/m_log2 in exponential.rs. If CPython uses a helper, use the corresponding Rust helper.

Applied to files:

  • src/err.rs
  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/math/exponential.rs : Copy CPython's explicit special case handling for IEEE specials (NaN, Inf, etc.) exactly, including logic for cases like NaN**0 = 1.

Applied to files:

  • src/err.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : Use mul_add in cmath functions for expressions like 1.0 + x * x, cross-product calculations (mul_add(s1.re, s2.im, -(s2.re * s1.im))), and squared terms to achieve bit-exact matching with CPython.

Applied to files:

  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/cmath/**/*.rs : For complex math (cmath module), copy special_type() enum and function, 7x7 special value tables (e.g., tanh_special_values), SPECIAL_VALUE macro usage, and complex-specific error handling from Modules/cmathmodule.c.

Applied to files:

  • src/math/integer.rs
📚 Learning: 2026-01-10T13:40:01.981Z
Learnt from: CR
Repo: RustPython/pymath PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-10T13:40:01.981Z
Learning: Applies to src/**/*test*.rs : All float functions must be tested with crate::test::EDGE_VALUES which includes zeros, infinities, NaNs with different payloads, subnormals, boundary values (MIN_POSITIVE, MAX, MIN), large values near infinity, and trigonometric special values (PI, PI/2, PI/4, TAU).

Applied to files:

  • src/math/integer.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build and test (windows-latest)
  • GitHub Check: Check exotic targets
  • GitHub Check: Build and test (macos-latest)
🔇 Additional comments (13)
.github/workflows/rust.yml (4)

34-35: Good choice switching to dtolnay/rust-toolchain.

The dtolnay/rust-toolchain action is actively maintained and simpler to use than the deprecated actions-rs/toolchain.


44-58: macOS cross-compilation checks look correct.

The conditional steps for Intel macOS (x86_64-apple-darwin) and iOS (aarch64-apple-ios) are properly gated with runner.os == 'macOS'.


60-113: Comprehensive exotic targets coverage.

The job covers a good variety of targets including 32-bit Linux, Android, ARM64, musl, FreeBSD, and WASM variants. Using cargo check instead of cargo build is appropriate for cross-compilation validation where you may not have the full toolchain.


115-124: Clippy job configured correctly.

Using -Dwarnings ensures the build fails on any Clippy warnings, which is a good practice for maintaining code quality.

src/err.rs (3)

88-107: is_error implementation follows coding guidelines correctly.

The function properly checks errno after libm calls and returns Result with Error::EDOM or Error::ERANGE. The underflow-to-zero handling (threshold < 1.5) matches CPython's behavior per bpo-46018.


3-6: safe fn _errno() syntax is valid and properly supported.

The safe fn declaration inside the unsafe extern "C" block is valid Rust syntax (RFC 3484, stabilized in Rust 1.82). Since the project targets edition 2024, this syntax is fully supported. The Windows errno pointer retrieval is correct.


8-12: Hardcoded errno values EDOM=33 and ERANGE=34 are universally correct across all platforms.

These values are standardized across POSIX systems (Linux, macOS, BSD variants, Android) and Windows. The codebase appropriately handles platform-specific errno access mechanisms via get_errno() and set_errno(), but the numeric constants themselves are invariant and require no exotic target verification.

src/math/integer.rs (6)

83-89: isqrt error handling follows coding guidelines correctly.

Returns Err(crate::Error::EDOM) for negative input, which aligns with the coding guidelines for error handling.


236-253: factorial error handling is correct.

Properly returns Err(crate::Error::EDOM) for negative input, matching Python's ValueError behavior.


655-678: comb implementation is correct.

  • Returns Err(crate::Error::EDOM) for negative n or k
  • Returns Ok(BigUint::zero()) when k > n (correct per combinatorics definition)
  • Uses symmetry optimization k.min(n - k) for efficiency

688-697: perm properly handles negative k in Option.

The pattern matching correctly handles the Some(k) if k < 0 case to return Err(crate::Error::EDOM).


712-856: Test functions comprehensively validate against Python.

The tests properly:

  • Compare Rust results with Python's math module
  • Check both Ok results and error cases
  • Use edge values to cover boundary conditions

1063-1133: Proptest coverage is thorough.

The property-based tests cover:

  • Small and large value ranges
  • Negative inputs for error cases
  • Various combinations for gcd/lcm/comb/perm

This provides good confidence in correctness across a wide input space.

@youknowone youknowone merged commit 1434545 into main Jan 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants